<?php
namespace app\controllers;

use fastphp\base\Controller;
use app\models\Problem;
use app\models\Record;
use app\models\User;

class ApiController extends Controller
{

    public function index()
    {
        $this->assign('status', false);
        $this->assign('message', '未知操作');
        $this->prepare();
        $this->render();
    }

    public function problem($id = 0)
    {
        $problem = (new Problem())->where(['id=?'], [$id])->fetch();
        $this->assign('status', (bool) $problem);
        $this->assign('data', $problem);
        $this->prepare();
        $this->render();
    }

    public function problemlist()
    {
        $problemlist = (new Problem())->list(@$_GET['page'], @$_GET['limit'], @$_GET['desc']);
        $this->assign('status', (bool) $problemlist);
        $this->assign('data', $problemlist);
        $this->prepare();
        $this->render();
    }

    public function userlogin()
    {
        $this->prepare();
        $this->assign('status', User::login($message));
        $this->assign('message', $message);
        $this->render();
    }
    
    public function record($id = 0)
    {
        $record = (new Record())->get($id);
        $this->assign('status', (bool) $record);
        $this->assign('data', $record);
        $this->prepare();
        $this->render();
    }
    
    
    public function getconfig($id = 0)
    {
        // Validation
        $id or die('?');
        // Read configs
        $record = (new Record())->get($id) or die('?');
        $config = $record;
        $config['compile'] = $this->findLanguage($record['language'], json_decode(file_get_contents('/var/www/judge/conf/compile.json'), true)['Languages']) or die('?');
        $config['compile']['exec'] = explode(' ', $config['compile']['exec']);
        $config['data'] = json_decode(file_get_contents('/var/www/judge/data/' . $record['problem'] . '/datalist.json'), true) or die('?');
        $config['compile']['progName'] = 'prog';
        if (isset($config['compile']['sourceSuffix']))
            $config['compile']['progName'] .= '.' . $config['compile']['sourceSuffix'];
        // [NN] Auto
        if (@$config['data']['auto'] === true) {
            $config['data']['testpoints'] = array();
            for ($i = $config['data']['begin']; $i <= $config['data']['end']; $i++) {
                array_push($config['data']['testpoints'], array('input'=>$i.@$config['data']['inSuffix'], 'output'=>$i.@$config['data']['outSuffix']));
            }
        }
        // Testpoints
        $tid = 0;
        foreach ($config['data']['testpoints'] as &$testpoint) {
            if (!isset($testpoint['time']))
                $testpoint['time'] = $config['data']['time'];
            if (!isset($testpoint['memory']))
                $testpoint['memory'] = $config['data']['memory'];
            if (!isset($testpoint['comparer']))
                $testpoint['comparer'] = $config['data']['comparer'];
            $testpoint['input'] = '/var/www/judge/data/' . $config['problem'] . '/' . $testpoint['input'];
            $testpoint['output'] = '/var/www/judge/data/' . $config['problem'] . '/' . $testpoint['output'];
            $testpoint['id'] = $tid++;
        }
        $this->assign('status', true);
        $this->assign('data', $config);
        $this->prepare();
        $this->render();
    }
    public function setrecord($id = 0)
    {
        if (isset($_POST['info'])) {
            $info = json_decode($_POST['info'], true);
            $result = (new Record())->where(['submitid='.$id])->update(['status'=>$info['status'], 'time'=>$info['time'], 'memory'=>$info['memory'], 'info'=>$_POST['info']]);
            $this->assign('status', (bool)$result);
            $this->assign('data', $_POST['info']);
            $this->prepare();
            $this->render();
        } else {
            die('?');
        }
    }
    private function findLanguage($language, $configs)
    {
        foreach($configs as $lang) {
            if ($lang['name'] == $language)
                return $lang;
        }
        return [];
    }
    
    private function prepare()
    {
        $this->assign('time', time());
    }
}